package com.liuda066.forumssm.controller;

import com.liuda066.forumssm.common.AppResult;
import com.liuda066.forumssm.model.Board;
import com.liuda066.forumssm.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/9/4 20:34
 */
@RestController
@Slf4j
@Api(tags = "板块接口")
@RequestMapping("/board")
public class BoardController {
    @Resource
    private IBoardService iBoardService;
    @Value("${forum.index.board-num:9}")
    private Integer indexBoardNum;

    @ApiOperation("创建板块")
    @PostMapping("/create")
    public AppResult create(@ApiParam("板块名称") @RequestParam(value = "name") @NonNull String name,
                            @ApiParam("排序") @RequestParam(value = "sort") @NonNull Integer sort) {
        Board board = new Board();
        board.setName(name);
        board.setSort(sort);

        iBoardService.create(board);

        return AppResult.success();
    }

    @ApiOperation("获取板块详情")
    @GetMapping("/getById")
    public AppResult<Board> getBoardInfo(@ApiParam("板块Id") @RequestParam(value = "id") @NonNull Long id) {
        Board result = iBoardService.selectById(id);
        return AppResult.success(result);
    }

    @ApiOperation("主页中显示的板块")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList() {
        System.out.println(indexBoardNum);
        List<Board> boards = iBoardService.selectByNum(indexBoardNum);
        return AppResult.success(boards);
    }
    @ApiOperation("获取所有正常状态的板块")
    @GetMapping("/allNormal")
    public AppResult<List<Board>> allNormal() {
        List<Board> boards = iBoardService.selectAllNormal();
        return AppResult.success(boards);
    }

}
